<template>
  <div>
    <el-container class="main-el-contarner">
      <el-main>
        <div class="kanbao-bigbox">
          <div class="righttop-search-box">
            <el-form
              :inline="true"
              :model="param"
              label-width="100px"
              @submit.native.prevent
            >
              <!-- <el-form-item label="事件等级：" label-width="90px">
              <el-select v-model="param.warnStatus" placeholder="事件等级" size="small" style="width:150px;">
                <el-option label="全部" value=""></el-option>
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item> -->
              <el-form-item label="" label-width="10px">
                <el-input
                  placeholder="请输入事件名称关键字检索"
                  v-model="param.name"
                  size="small"
                  clearable
                  style="width: 350px"
                  @keyup.enter.native="onSearch"
                >
                  <i slot="prefix" class="el-input__icon el-icon-search"></i>
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="small" @click="onSearch">查找</el-button>
              </el-form-item>

              <el-form-item style="float: right">
                <el-button size="mini" type="primary" plain @click="handleAdd"
                  ><i class="el-icon-download"></i> 导出数据</el-button
                >
              </el-form-item>
            </el-form>
          </div>
          <div class="container-box">
            <el-table
              :data="tabledatas"
              border
              stripe
              max-height="480px"
              :header-cell-style="{
                background: '#cfcfcf',
                color: '#000000',
                'text-align': 'center',
              }"
              :cell-style="{ 'text-align': 'center' }"
              ref="talbes"
              style="width: 100%"
              size="small"
              @selection-change="selectionChange"
            >
              <el-table-column
                type="selection"
                header-align="center"
                width="60"
              ></el-table-column>
              <el-table-column prop="event_name" label="事件名称"> </el-table-column>
              <el-table-column prop="event_description" label="事件描述">
              </el-table-column>
              <el-table-column prop="is_inform" label="是否通知">
                <template #default="scope">
                  <font v-if="scope.row.is_inform == '1'">是</font>
                  <font v-if="scope.row.is_inform == '0'">否</font>
                </template>
              </el-table-column>
              <el-table-column prop="is_turn_to_alarm" label="是否告警">
                <template #default="scope">
                  <font v-if="scope.row.is_turn_to_alarm == '1'">是</font>
                  <font v-if="scope.row.is_turn_to_alarm == '0'">否</font>
                </template>
              </el-table-column>
              <el-table-column prop="is_to_control" label="是否联动设备控制">
                <template #default="scope">
                  <font v-if="scope.row.is_to_control == '1'">是</font>
                  <font v-if="scope.row.is_to_control == '0'">否</font>
                </template>
              </el-table-column>
              <el-table-column
                prop="create_date"
                label="创建时间"
                :formatter="dateFormat"
              >
              </el-table-column>
              <el-table-column
                prop="active_time"
                label="触发时间"
                :formatter="dateFormat"
              >
              </el-table-column>
            </el-table>

            <div class="pagebox">
              <el-pagination
                @size-change="sizeChange"
                @current-change="currentChange"
                :current-page="param.pageNo"
                :page-sizes="[10, 30, 50, 100, 500]"
                :page-size="param.limit"
                layout="total, sizes, prev, pager, next, jumper"
                :total="dataTotal"
              >
              </el-pagination>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import SideHeader from "@/views/inc/SideHeader";
import Navlanmu from "@/views/inc/Navlanmu";
import LeftMenuMessage from "@/views/inc/LeftMenuMessage";
import { getDateTimes, numFormat, getDateWeek, downloadCallback } from "@/utils/index.js";
import { getlist, getexpro } from "@/api/message/list";
import moment from "moment";

export default {
  name: "EventRecord",
  components: { SideHeader, Navlanmu, LeftMenuMessage },
  data() {
    return {
      loadingdata1: false,
      isloading: false,
      param: {
        funId: "msg_event_records",
        baseSQL: "",
        warnStatus: "",
        name: "",
        offset: 0,
        pageNo: 1,
        limit: 10,
      },
      dataTotal: 0,
      tabledatas: [],
      selectionChangeData: [],
    };
  },
  mounted() {
    //
    this.getDatas();
  },
  created() {
    this.isloading = false;
  },
  methods: {
    nodeClick(type, data) {
      this.loadingdata1 = true;
      console.log("nodeClick", type, data);
      this.searchForm = data;
      this.getEnergyQushi();
      this.getEnergyDeptRanking();
      this.getEnergylevelCount();
    },
    // 复选框按钮
    selectionChange() {
      // console.log("selectionChange");
      this.selectionChangeData = e;
    },
    //导出按钮
    handleAdd() {
      let fromData = {
        exportFields: [],
        ids: [],
        funId: "msg_event_records",
      };
      //动态获取表头
      let fields = [];
      this.$refs.talbes.$children.forEach((item) => {
        if (item.label != undefined && item.prop != undefined) {
          fields.push({
            title: item.label,
            value: item.prop,
          });
        }
        fromData.exportFields = JSON.stringify(fields);
      });
      //获取主键ID
      if (this.selectionChangeData.length != 0) {
        this.selectionChangeData.forEach((item, index) => {
          if (index != 0) {
            fromData.ids += ";";
          }
          fromData.ids += item.event_record_id;
        });
      } else {
        this.tabledatas.forEach((item, index) => {
          if (index != 0) {
            fromData.ids += ";";
            // console.log('fromData.ids',fromData.ids)
          }
          fromData.ids += item.event_record_id;
        });
      }
      downloadCallback(fromData, this.selectionChangeData.length);
    },
    // 数据搜索按钮
    onSearch() {
      this.param.pageNo = 1;
      this.getDatas();
    },
    getDatas() {
      if (this.param.name == "") {
        this.param.baseSQL = "";
      } else {
        this.param.baseSQL = 'event_name like"%' + this.param.name + '%"';
      }
      getlist(this.param).then((res) => {
        if (res.status == "200") {
          let resdata = res.data;
          this.tabledatas = resdata.rows;
          this.dataTotal = resdata.total;
        }
      });
    },

    indexMethod(index) {
      return (this.param.pageNo - 1) * this.param.limit + index + 1;
    },
    sizeChange(val) {
      this.param.limit = val;
      this.getDatas();
    },
    currentChange(val) {
      this.param.pageNo = val;
      this.param.offset = (val - 1) * this.param.limit;
      this.getDatas();
    },
    dateFormat(row, column, cellValue, index) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
  },
};
</script>
<style>
.kanbao-bigbox .el-loading-spinner .el-loading-text {
  color: #666 !important;
}
</style>
<style scoped>
.el-main {
  padding: 0px !important;
}

.kanbao-bigbox {
  margin-top: 0px;
}
</style>
<style scoped>
.righttop-search-box {
  background: #f8f8f8;
  padding: 8px 20px 7px 20px;
  border-bottom: 1px solid #ececec;
  position: fixed;
  top: 0;
  width: calc(100% - 55px);
  z-index: 4;
}

.righttop-search-box .el-form-item {
  margin-bottom: 0px !important;
}

.container-box {
  padding: 10px;
  margin: 50px 0;
}

.pagebox {
  text-align: right;
  padding: 10px;
  position: fixed;
  bottom: 0;
  right: 0;
  width: 100%;
  background-color: #ececec;
  z-index: 4;
}
</style>
